//
//  SecondViewController.m
//  EsayHandwritingSignature
//
//  Created by N-X on 2017/11/27.
//  Copyright © 2017年 liang. All rights reserved.
//

#import "NXSignatureController.h"
#import "NXSignatureView.h"
#define RGB(__R, __G, __B) [UIColor colorWithRed:(__R) / 255.0f green:(__G) / 255.0f blue:(__B) / 255.0f alpha:1.0]

@interface NXSignatureController ()
@property (nonatomic ,strong)UIView * containerView;
@property (nonatomic ,strong)NXSignatureView  * nxsignatureView;
@property (nonatomic ,strong)UIView * seperatorView;
@property (nonatomic ,strong)UIButton * leftButton;
@property (nonatomic ,strong)UIButton * rightButton;
@property (nonatomic ,strong)UIButton * submitButton;
@property (nonatomic ,strong)UILabel * headerLabel;
@end

@implementation NXSignatureController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self.view addSubview:self.containerView];
    [self.containerView addSubview:self.headerLabel];
    [self.containerView addSubview:self.seperatorView];
    [self.containerView addSubview:self.nxsignatureView];
    [self.containerView addSubview:self.rightButton];
    [self.containerView addSubview:self.leftButton];
    [self.containerView addSubview:self.submitButton];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    [self.navigationController setNavigationBarHidden:YES animated:animated];
    [UIView animateWithDuration:0.25 animations:^{
        [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight];
        self.view.transform = CGAffineTransformMakeRotation(M_PI/2);
        CGRect frame = [UIScreen mainScreen].applicationFrame;
        //            self.view.bounds = CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height);
        self.view.frame = CGRectMake(0, 0, frame.size.height, frame.size.width);
        NSLog(@"isLandSpace======== bounds:%@ , frame:%@ ,screenFrame:%@ ",NSStringFromCGRect(self.view.bounds),NSStringFromCGRect(frame),NSStringFromCGRect([UIScreen mainScreen].bounds));
    }];
}

- (void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
    [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight];
    self.view.transform = CGAffineTransformMakeRotation(M_PI/2);
    CGRect frame = [UIScreen mainScreen].applicationFrame;
    //            self.view.bounds = CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height);
    self.view.frame = CGRectMake(0, 0, frame.size.height, frame.size.width);
}

- (BOOL)prefersStatusBarHidden
{
    return true;
}

- (void)viewDidLayoutSubviews
{
    [super viewDidLayoutSubviews];
    self.containerView.frame = CGRectMake(0, 0, self.view.frame.size.height, self.view.frame.size.width);
    self.rightButton.frame = CGRectMake(self.containerView.frame.size.width - 50, 0, 44, 44);
    self.seperatorView.frame = CGRectMake(0, CGRectGetMaxY(self.rightButton.frame), self.containerView.frame.size.width, 1);
    self.nxsignatureView.frame = CGRectMake(0, 45, self.containerView.frame.size.width, self.containerView.frame.size.height - 44 - 44);
    self.submitButton.frame = CGRectMake(0, self.containerView.frame.size.height -44, self.containerView.frame.size.width, 44);
    self.headerLabel.frame = CGRectMake(0, 0, self.containerView.frame.size.width, 44);
}

#pragma mark - setter && getter

- (UIView *)containerView
{
    if (!_containerView) {
        _containerView = [UIView new];
    }
    return _containerView;
}
- (UIView *)seperatorView
{
    if (!_seperatorView) {
        _seperatorView = [[UIView alloc] init];
        _seperatorView.backgroundColor = RGB(238, 238, 238);
    }
    return _seperatorView;
}
- (NXSignatureView *)nxsignatureView
{
    if (!_nxsignatureView) {
        _nxsignatureView = [[NXSignatureView alloc]init];
        _nxsignatureView.backgroundColor = [UIColor whiteColor];
//        _nxsignatureView.delegate = self;
    }
    return _nxsignatureView;
}

- (UIButton *)submitButton
{
    if (!_submitButton) {
        _submitButton = [self createButtonWithFrame:CGRectMake(0, self.containerView.frame.size.height -44, self.containerView.frame.size.width, 44) title:@"提交" textColor:[UIColor colorWithRed:255.0/255 green:255.0/255 blue:255.0/255 alpha:0.5] font:[UIFont systemFontOfSize:15] image:nil backGroundImage:nil backGroundColor:[UIColor colorWithRed:0.1529 green:0.7765 blue:0.7765 alpha:1.0] target:self action:@selector(okAction)];
    }
    return _submitButton;
}

- (UIButton *)rightButton
{
    if (!_rightButton) {
        _rightButton = [self createButtonWithFrame:CGRectMake(self.containerView.frame.size.width - 50, 0, 44, 44) title:@"清除" textColor:[UIColor darkGrayColor] font:[UIFont systemFontOfSize:15] image:nil backGroundImage:nil backGroundColor:nil target:self action:@selector(onClear)];
    }
    return _rightButton;
}

- (UIButton *)leftButton
{
    if (!_leftButton) {
        _leftButton = [self createButtonWithFrame:CGRectMake(6, 0, 100, 44) title:@"取消" textColor:[UIColor darkGrayColor] font:[UIFont systemFontOfSize:15] image:nil backGroundImage:nil backGroundColor:nil target:self action:@selector(cancelAction)];
    }
    return _leftButton;
}

- (UILabel *)headerLabel
{
    if (!_headerLabel) {
        _headerLabel = [[UILabel alloc] init];
        _headerLabel.backgroundColor = [UIColor whiteColor];
        _headerLabel.textAlignment = NSTextAlignmentCenter;
        _headerLabel.textColor = [UIColor colorWithRed:0.3258 green:0.3258 blue:0.3258 alpha:1.0];
        _headerLabel.font = [UIFont systemFontOfSize:15];
        _headerLabel.text = @"此处手写签名: 工整书写";
    }
    return _headerLabel;
}

#pragma mark - 创建button

- (UIButton *)createButtonWithFrame:(CGRect)frame title:(NSString *)title textColor:(UIColor *)textColor font:(UIFont *)font image:(NSString *)image backGroundImage:(NSString *)backGroundImage backGroundColor:(UIColor *)backGroundColor target:(id)target action:(SEL)action;
{
    UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = frame;
    [button setTitle:title forState:UIControlStateNormal];
    button.titleLabel.font = font;
    [button setImage:[UIImage imageNamed:image] forState:UIControlStateNormal];
    [button setBackgroundImage:[UIImage imageNamed:backGroundImage] forState:UIControlStateNormal];
    [button setTitleColor:textColor forState:UIControlStateNormal];
    button.backgroundColor = backGroundColor;
    [button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    return button;
}

#pragma mark - 点击事件

//清除
- (void)onClear {
    //    [signatureView clear];
    [self.nxsignatureView clear];
    [self.submitButton setTitleColor:[UIColor colorWithRed:255.0/255 green:255.0/255 blue:255.0/255 alpha:0.5] forState:UIControlStateNormal];
}

- (void)okAction
{
    [self.nxsignatureView sure];
    if(self.nxsignatureView.SignatureImg)
    {
        NSLog(@"haveImage");
        if (self.signatureVCBlock) {
            self.signatureVCBlock(self.nxsignatureView.SignatureImg);
        }
        [self dismissViewControllerAnimated:YES completion:nil];
    }
    else
    {
        NSLog(@"NoImage");
    }
}

- (void)cancelAction
{
    [self dismissViewControllerAnimated:YES completion:nil];
}
@end
